import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import store from './store'
import ViewUIPlus from 'view-ui-plus'
import 'view-ui-plus/dist/styles/viewuiplus.css'
import "layui/dist/css/layui.css"
import "layui/dist/layui.js"
import axios from 'axios'
const modules = import.meta.glob('./views/admin/*/*.vue')

const app = createApp(App)
app.config.globalProperties.$axios = axios
axios.interceptors.request.use(config => {
  let token = localStorage.getItem('token');
  if (token) {
    config.headers['token'] = token;
  }
  return config;
},
  error => {
    return Promise.reject(error.response);
  })

router.beforeEach((to, from, next) => {
  let fullPath = to.path.replace(/^\/admin\//, ''), temp = fullPath.split('/'), token = localStorage.getItem('token'), menu = localStorage.getItem('menu');
  if ((!token || !menu) && fullPath != 'login') {
    router.push({ path: '/admin/login' }).catch(error => console.log(error));
    localStorage.clear();
  } else if (to.matched.length === 0) {
    if (temp.length == 2) {
      router.addRoute({
        name: fullPath.replace(/\//g, '_'),
        path: '/admin/' + fullPath,
        component: modules['./views/admin/public/' + (/Add/.test(fullPath) ? 'add' : 'list') + '.vue']
      })
      router.push({ name: fullPath.replace(/\//g, '_'), query: to.query }).catch(error => console.log(error));
    } else {
      router.push({ name: 'error404' }).catch(error => console.log(error));
    }
  }
  next();
})
app.config.globalProperties.$mymessage = function (type, title, onOk, onNo) {
  app.config.globalProperties.$Modal[type]({
    title: title,
    onOk: () => {
      try {
        onOk();
      } catch (e) {

      }
    },
    onCancel: () => {
      try {
        onNo();
      } catch (e) {

      }
    }
  });
}
app.config.globalProperties.$captcha = function () {
  let captcha = document.getElementById('captcha');
  if (captcha) {
    captcha.src = '/api/index/login/captcha?r=' + Math.random();
  }
}
app.config.globalProperties.$submit = (url, param, success, error) => {
  layer.load()
  let handle = (res, success, error) => {
    try {
      layer.closeAll()
    } catch (error) {
      
    }
    if (res.data.status == '10000') {
      if (typeof success != 'undefined') {
        success(res)
      }
    } else if (res.redirect) {
      app.config.globalProperties.$jumpUrl(res.redirect)
      return
    } else {
      if (res.data.status == '30001') {
        app.config.globalProperties.$jumpUrl('/admin/login')
        return
      }
      if (typeof error == 'function') {
        error(res)
        return
      }
      app.config.globalProperties.$Message.error({
        content: res.data.msg,
        duration: 5
      })
      app.config.globalProperties.$captcha();
    }
  }
  let method = 'post'
  try {
    method = param.method
  } catch (error) {

  }
  url = '/api' + (/^\//.test(url) ? '' : '/') + url;
  if (method == 'get') {
    app.config.globalProperties.$axios.get(url, param).then((res) => {
      handle(res, success, error)
    }).catch((error) => {
      layer.closeAll()
      console.log(error);
      app.config.globalProperties.$Message.error({ content: '服务器开小差了，请稍后再试' });
    });
  } else {
    app.config.globalProperties.$axios.post(url, param).then((res) => {
      handle(res, success, error)
    }).catch((error) => {
      console.log(error);
      try {
        layer.closeAll()
      } catch (error) {
        
      }
      app.config.globalProperties.$Message.error({ content: '服务器开小差了，请稍后再试' });
    });
  }
}
app.config.globalProperties.$jumpUrl = (url) => {
  if (!url) {
    url = localStorage.getItem('jumpUrl') ? localStorage.getItem('jumpUrl') : '/';
  }
  router.push({ path: url }).catch(error => {
    console.log(error);
  });
}

app.config.globalProperties.$getmenu = function (needgo, func) {
  try {
    app.config.globalProperties.$submit('/admin/common/getUserMenuList', {}, res => {
      localStorage.setItem('menu', JSON.stringify(res.data.data));
      let temp, temp1 = { index2: 'index2' };
      for (let k in res.data.data) {
        if (typeof res.data.data[k].sub != 'undefined') {
          for (let k1 in res.data.data[k].sub) {
            temp = res.data.data[k].sub[k1].url.replace(/\//g, '_');
            temp1[temp] = 'menu' + res.data.data[k].sub[k1].pid;
          }
        }
      }
      localStorage.setItem('menu1', JSON.stringify(temp1));
      if (needgo === true) {
        app.config.globalProperties.$router.push({ path: '/' }).catch(() => { });
      }
      // layer.closeAll()
      // this.$router.push({path: '/'}).catch(() => {});
    })
  } catch (error) {
    console.log(error)
  }
}
app
  .use(router)
  .use(ViewUIPlus)
  .mount('#app')
